package com.example.meterialproject.activitys.coordinator

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.example.meterialproject.R
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.databinding.ActivityCoordinatorDemo1Binding
import com.example.meterialproject.exts.WHLayout
import com.example.meterialproject.exts.dp

/**
 *
 * @ClassName: CoordinatorDemo1Activity
 * @Author: android 超级兵
 * @CreateDate: 4/13/22$ 7:13 PM$
 * TODO
 */

class CoordinatorDemo1Activity : BaseActivity<ActivityCoordinatorDemo1Binding>() {

    private val coordinatorView by lazy {
        findViewById<CoordinatorLayout>(R.id.coordinatorView)
    }

    // 是否添加 View
    private var isAdd = true
    var tempTv: TextView? = null

    @SuppressLint("SetTextI18n")
    fun onImageClick(v: View) {
        if (isAdd) {
            val padding = binding.root.paddingLeft + binding.root.paddingRight
            val width = 100.dp(this@CoordinatorDemo1Activity).toInt()
            val height = width
            val tvX = (binding.root.right - width - padding) * 1.0f
            val tvY = 0f

            // 添加
            TextView(this).apply {
                setBackgroundColor(Color.RED)
                // 设置宽高
                WHLayout(width, height)
                // 文字居中
                gravity = Gravity.CENTER
                x = tvX
                y = tvY
                text = "添加的 view"
                coordinatorView.addView(this)
            }.also { tempTv = it }
        } else {
            // 删除
            coordinatorView.removeView(tempTv ?: return)
        }
        isAdd = !isAdd
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}